<script setup>
import { ref } from 'vue'
import { Edit, Delete } from '@element-plus/icons-vue'

// 导入下拉菜单组件
import channelSelect from './components/ChannelSelect.vue'

// 导入格式化时间的函数api
import { formatTime } from '@/utils/formal.js'

// 导入获取用户信息 && 删除文章
import { artGetMangeService, DelMangerArticel } from '@/api/articel.js'

// 导入组件
import ArticleEidt from '@/views/article/components/ArticleEidt.vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// 给组件传值
defineProps({
  modelValue: {
    type: [Number, String]
  }
})

const total = ref(0)
const loading = ref(true)
// 需要传的参数
const params = ref({
  pagenum: 1,
  pagesize: 5,
  cate_id: '',
  state: ''
})
// 渲染列表的数据
const articleList = ref([])
// 一进入页面我们就做渲染
const GetMangList = async () => {
  const res = await artGetMangeService(params.value)
  loading.value = false
  articleList.value = res.data.data
  total.value = res.data.total // 总共有多少页
  console.log(res.data)
}
GetMangList()

// 点击跳转页面的时候  handleCurrentChange
const handleCurrentChange = (page) => {
  params.value.pagenum = page
  GetMangList()
}
// 点击一页多少个的时候handleSizeChange
const handleSizeChange = (size) => {
  params.value.pagesize = size // 每一页有多少个
  params.value.pagenum = 1 // 当前页
  GetMangList()
}

// 搜索的逻辑
const onSearch = () => {
  params.value.pagenum = 1
  GetMangList()
}

// 重置的逻辑
const onReset = () => {
  params.value.pagenum = 1
  params.value.cate_id = ''
  params.value.state = ''
  GetMangList() 
}

// 编辑的弹窗
const atriclEidt = ref()
const Eidt = () => {
  atriclEidt.value.open({})
}

// 编辑圆形按钮 
const onEditChannel = (row) => {
  atriclEidt.value.open(row)
}

// 侦听 Eidt 组件返回来的 onSuccess 事件
const onSuccess = (type) => {
    if (type === 'add') {
    // 如果是添加，需要跳转渲染最后一页，编辑直接渲染当前页
    const lastPage = Math.ceil((total.value + 1) / params.value.pagesize)
    params.value.pagenum = lastPage
  }
  GetMangList() 
}

// 删除---圆形 按钮
const onDelChannel = async (row) => {
    await ElMessageBox.confirm('你确认删除该分类信息吗？', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  DelMangerArticel(row.id)
  GetMangList()
  ElMessage.success('删除成功')
}

</script>
<template>
  <PageContainer title="文章管理" v-loading="loading">
    <template #extra>
      <el-button type="primary" @click="Eidt">发布文章</el-button>
    </template>
    <!-- ----------  文章分类 -------- -->
    <el-form inline>
      <el-form-item label="文章分类：">
          <!-- 下拉菜单 -->
        <channelSelect v-model="params.cate_id"></channelSelect>
          <!-- -------------------- -->
      </el-form-item>
      <!-- ----------  发布状态 -------- -->
      <el-form-item label="发布状态：">
        <el-select v-model="params.state" placeholder="请选择" style="width: 240px">
          <el-option label="已发布" value="已发布"></el-option>
          <el-option label="草稿" value="草稿"></el-option>
        </el-select>
      </el-form-item>
      <!-- ------------  搜索&重置按钮  ------------ -->
      <el-form-item>
        <el-button type="primary" @click="onSearch">搜索</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格部分 -->
    <el-table :data="articleList" height="400" style="width: 100%">
      <el-table-column prop="title" label="文章标题" width="180" >
        <template #default="{ row }">
            <el-link type="primary">{{ row.title }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="cate_name" label="分类" width="180" />
      <el-table-column label="发表时间">
        <template #default="{ row }">
          {{ formatTime(row.pub_date) }}
        </template>
      </el-table-column>
      <el-table-column prop="state" label="状态" />
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button
            :icon="Edit"
            circle
            plain
            type="primary"
            @click="onEditChannel(row)"
          >
          </el-button>
          <el-button
            :icon="Delete"
            circle
            plain
            type="danger"
            @click="onDelChannel(row)"
          >
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="demo-pagination-block">
      <el-pagination
        style="justify-content: flex-end"
        v-model:current-page="params.pagenum"
        v-model:page-size="params.pagesize"
        :page-sizes="[2, 5, 10]"
        :size="size"
        :disabled="disabled"
        :background="ture"
        layout="jumper, total, sizes, prev, pager, next, "
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <!-- 侦听 onSuccess 事件 -->
    <ArticleEidt ref="atriclEidt"  @success="onSuccess"></ArticleEidt>
  </PageContainer>
</template>

<style lang="scss">
.demo-pagination-block {
  justify-content: flex-end;
}
</style>
